﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_EShop._Class.BLL;
using V5_Common;
using System.Data;
using V5_EShop._Class.Model;
using V5_BLL;
using V5_Model;
using System.IO;

namespace V5_EShop.Admin.GZProduct
{
    public partial class Admin_Product : System.Web.UI.Page
    {
        const string vSearch = "vProduct";
        #region Bc_Product开始
        BLLGZProduct bllGZProduct = new BLLGZProduct();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string action = InputHelper.SetString(Request["action"]);
                this.Bind_DropDownClassList();
                switch (action)
                {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;

                        this.txtKeepQulityDate.Text = DateTime.Now.ToString();
                        this.txtPromotionsSDate.Text = DateTime.Now.ToString();
                        this.txtPromotionsEDate.Text = DateTime.Now.ToString();
                        this.txtLastInDate.Text = DateTime.Now.ToString();
                        break;
                    default:
                        this.Bind_DataList();
                        break;
                }
            }
        }

        #region 增删改
        private void Bind_DataList()
        {
            this.MultiViewList.ActiveViewIndex = 0;
            int RecordCount = 0;
            string strWhere = string.Empty;
            if (ViewState[vSearch] != null)
            {
                strWhere = InputHelper.SetString(ViewState[vSearch]);
            }
            DataTable dt = bllGZProduct.GetList(Pager.PageSize, Pager.CurrentPageIndex, strWhere, string.Empty, ref RecordCount).Tables[0];
            Pager.RecordCount = RecordCount;
            this.gvList.DataSource = dt;
            this.gvList.DataBind();
        }

        private void Bind_DataEdit(Guid ID)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelGZProduct model = bllGZProduct.GetModel(ID);
            this.hfID.Value = model.ID.ToString();
            this.ddlProductTypeID.SelectedIndex =
                this.ddlProductTypeID.Items.IndexOf(
                    this.ddlProductTypeID.Items.FindByValue(model.ProductTypeID.ToString())
                );
            this.txtSerialNo.Text = model.SerialNo ?? "";
            this.txtBarCode.Text = model.BarCode ?? "";
            this.txtPackageBarCode.Text = model.PackageBarCode ?? "";
            this.txtProductName.Text = model.ProductName ?? "";
            this.txtProductShortCutName.Text = model.ProductShortCutName ?? "";
            this.txtProductShortName.Text = model.ProductShortName ?? "";
            this.ddlUnitID.SelectedIndex =
                this.ddlUnitID.Items.IndexOf(
                    this.ddlUnitID.Items.FindByValue(model.UnitID.ToString())
                );
            this.ddlPackageUnitID.SelectedIndex =
                this.ddlPackageUnitID.Items.IndexOf(
                    this.ddlPackageUnitID.Items.FindByValue(model.PackageUnitID.ToString())
                );
            this.txtPackageSpec.Text = model.PackageSpec.ToString();
            this.txtManufactureName.Text = model.ManufactureName ?? "";
            this.txtMemberPrice.Text = model.MemberPrice.ToString();
            this.txtSalePrice1.Text = model.SalePrice1.ToString();
            this.txtSalePrice2.Text = model.SalePrice2.ToString();
            this.txtSalePrice3.Text = model.SalePrice3.ToString();
            this.txtSalePrice4.Text = model.SalePrice4.ToString();
            this.txtSalePiecePrice1.Text = model.SalePiecePrice1.ToString();
            this.txtSalePiecePrice2.Text = model.SalePiecePrice2.ToString();
            this.txtSalePIecePrice3.Text = model.SalePIecePrice3.ToString();
            this.txtSalePiecePrice4.Text = model.SalePiecePrice4.ToString();
            this.txtMemberDiscont.Text = model.MemberDiscont.ToString();
            this.txtMaxSalePrice.Text = model.MaxSalePrice.ToString();
            this.txtMinSalePrice.Text = model.MinSalePrice.ToString();
            this.txtMinSalePiecePrice.Text = model.MinSalePiecePrice.ToString().Trim() == "" ? "0.0000" : model.MinSalePiecePrice.ToString();
            this.txtMaxSalePiecePrice.Text = model.MaxSalePiecePrice.ToString().Trim() == "" ? "0.0000" : model.MaxSalePiecePrice.ToString();
            this.chkIntegralSign.Checked = model.IntegralSign.Value;
            this.txtIntegralRate.Text = model.IntegralRate.ToString();
            this.chkNoStockCanSales.Checked = model.NoStockCanSales.Value;
            //this.ddlPriceArithmeticID.SelectedIndex =
            //    this.ddlPriceArithmeticID.Items.IndexOf(
            //        this.ddlPriceArithmeticID.Items.FindByValue(model.PriceArithmeticID.ToString())
            //    );
            this.txtMaxInPrice.Text = model.MaxInPrice.ToString();
            this.txtMaxInPiecePrice.Text = model.MaxInPiecePrice.ToString();
            this.txtMinInPrice.Text = model.MinInPrice.ToString();
            this.txtMinInPiecePrice.Text = model.MinInPiecePrice.ToString();
            this.txtStockInPrice.Text = model.StockInPrice.ToString();
            this.txtStockInPiecePrice.Text = model.StockInPiecePrice.ToString();
            this.txtMaxStockNum.Text = model.MaxStockNum.ToString();
            this.txtMinStockNum.Text = model.MinStockNum.ToString();
            this.txtAverageInPrice.Text = model.AverageInPrice.ToString();
            this.txtTotalNum.Text = model.TotalNum.ToString();
            this.txtTotalMoney.Text = model.TotalMoney.ToString();
            this.txtKeepQulityDate.Text = model.KeepQulityDate.ToString();
            this.txtSpecMode.Text = model.SpecMode ?? "";
            this.txtUserfulLife.Text = model.UserfulLife.ToString();
            this.txtProducingArea.Text = model.ProducingArea ?? "";
            this.chkDisable.Checked = model.Disable.Value;
            this.chkIsEcalled.Checked = model.IsEcalled.Value;
            this.txtPromotionsPrice.Text = model.PromotionsPrice.ToString();
            this.txtPromotionsSDate.Text = model.PromotionsSDate.ToString();
            this.txtPromotionsEDate.Text = model.PromotionsEDate.ToString();
            this.ddlProviderID.SelectedIndex =
                this.ddlProviderID.Items.IndexOf(
                    this.ddlProviderID.Items.FindByValue(model.ProviderID.ToString())
                );
            this.txtLastInDate.Text = model.LastInDate.ToString();
            this.txtLastInPrice.Text = model.LastInPrice.ToString();
            this.txtSellerRate.Text = model.SellerRate.ToString();
            this.txtUserRate.Text = model.UserRate.ToString();
            this.txtRecommendedRate.Text = model.RecommendedRate.ToString();
            this.txtGiftsBarCode.Text = model.GiftsBarCode ?? "";
            this.txtGiftsIntegral.Text = model.GiftsIntegral.ToString();
            this.chkIsGifts.Checked = model.IsGifts.Value;
            this.ddlBrandID.SelectedIndex =
                this.ddlBrandID.Items.IndexOf(
                    this.ddlBrandID.Items.FindByValue(model.BrandID.ToString())
                );
            this.txtUseDay.Text = model.UseDay.ToString();
            this.chkIsShow.Checked = model.IsShow.Value;
            this.txtRemark.Text = model.Remark ?? "";
            this.txtRemark1.Text = model.Remark1 ?? "";
            this.txtRemark2.Text = model.Remark2 ?? "";
            this.txtRemark3.Text = model.Remark3 ?? "";
            this.txtRemark4.Text = model.Remark4 ?? "";
            this.txtRemark5.Text = model.Remark5 ?? "";
            this.txtRemark6.Text = model.Remark6 ?? "";
            //this.txtImage.Text = model.Image.ToString();
            this.txtProductDetails.Text = model.ProductDetails ?? "";

            this.hfProductFilePath.Value = model.PicturePath ?? "";

            this.chkDirectMarketing.Checked = model.DirectMarketing ?? false;
            this.chkSpecialofferings.Checked = model.Specialofferings ?? false;
            this.txtOid.Text = model.Oid.ToString();

            this.btnSave.Text = "保存修改";
        }

        private void Bind_Delete(Guid id)
        {
            bllGZProduct.Delete(id);
            Bind_DataList();
        }
        #endregion



        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_DataList();
        }

        protected void btnSearchType_Click(object sender, EventArgs e)
        {
            string opType = InputHelper.SetString(this.ddlSearchType.SelectedValue);
            if (opType == "0")
            {
                PrHelper.RegClientAlert(this, "请选择一个操作类型!");
                return;
            }
            foreach (System.Web.UI.WebControls.GridViewRow item in this.gvList.Rows)
            {
                CheckBox chk = (CheckBox)item.FindControl("chkitem");
                HiddenField hfID = (HiddenField)item.FindControl("hfID");
                Guid id = new Guid(hfID.Value);
                if (chk.Checked)
                {
                    switch (opType)
                    {
                        case "del":
                            bllGZProduct.Delete(id);
                            break;
                        default:
                            break;
                    }
                }
            }
            this.Bind_DataList();
        }

        protected void txtSearchTitle_Click(object sender, EventArgs e)
        {
            string sTitle = InputHelper.SetString(this.txtSearchTitle.Text);
            if (sTitle != "")
            {
                ViewState[vSearch] = " And ProductName like '%" + sTitle + "%' ";
            }
            Bind_DataList();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string ID = this.hfID.Value;
            string ProductTypeID = this.ddlProductTypeID.SelectedValue;
            string SerialNo = this.txtSerialNo.Text;
            string BarCode = this.txtBarCode.Text;
            string PackageBarCode = this.txtPackageBarCode.Text;
            string ProductName = this.txtProductName.Text;
            string ProductShortCutName = this.txtProductShortCutName.Text;
            string ProductShortName = this.txtProductShortName.Text;
            string UnitID = this.ddlUnitID.SelectedValue == "0" || this.ddlUnitID.SelectedValue == "" ? "00000000-0000-0000-0000-000000000000" : this.ddlUnitID.SelectedValue;
            string PackageUnitID = this.ddlPackageUnitID.SelectedValue == "0" || this.ddlPackageUnitID.SelectedValue == "" ? "00000000-0000-0000-0000-000000000000" : this.ddlPackageUnitID.SelectedValue; ;
            string PackageSpec = this.txtPackageSpec.Text;
            string ManufactureName = this.txtManufactureName.Text;
            string MemberPrice = this.txtMemberPrice.Text;
            string SalePrice1 = this.txtSalePrice1.Text;
            string SalePrice2 = this.txtSalePrice2.Text;
            string SalePrice3 = this.txtSalePrice3.Text;
            string SalePrice4 = this.txtSalePrice4.Text;
            string SalePiecePrice1 = this.txtSalePiecePrice1.Text;
            string SalePiecePrice2 = this.txtSalePiecePrice2.Text;
            string SalePIecePrice3 = this.txtSalePIecePrice3.Text;
            string SalePiecePrice4 = this.txtSalePiecePrice4.Text;
            string MemberDiscont = this.txtMemberDiscont.Text;
            string MaxSalePrice = this.txtMaxSalePrice.Text;
            string MinSalePrice = this.txtMinSalePrice.Text;
            string MinSalePiecePrice = this.txtMinSalePiecePrice.Text;
            string MaxSalePiecePrice = this.txtMaxSalePiecePrice.Text;
            bool IntegralSign = this.chkIntegralSign.Checked;
            string IntegralRate = this.txtIntegralRate.Text;
            bool NoStockCanSales = this.chkNoStockCanSales.Checked;
            string PriceArithmeticID = "00000000-0000-0000-0000-000000000000"; //this.txtPriceArithmeticID.Text;
            string MaxInPrice = this.txtMaxInPrice.Text;
            string MaxInPiecePrice = this.txtMaxInPiecePrice.Text;
            string MinInPrice = this.txtMinInPrice.Text;
            string MinInPiecePrice = this.txtMinInPiecePrice.Text;
            string StockInPrice = this.txtStockInPrice.Text;
            string StockInPiecePrice = this.txtStockInPiecePrice.Text;
            string MaxStockNum = this.txtMaxStockNum.Text;
            string MinStockNum = this.txtMinStockNum.Text;
            string AverageInPrice = this.txtAverageInPrice.Text;
            string TotalNum = this.txtTotalNum.Text;
            string TotalMoney = this.txtTotalMoney.Text;
            DateTime result;
            DateTime KeepQulityDate = DateTime.TryParse(this.txtKeepQulityDate.Text, out result) ? result : DateTime.Now;
            DateTime CreateDate = DateTime.Now;// Parse(this.txtCreateDate.Text);
            string SpecMode = this.txtSpecMode.Text;
            string UserfulLife = this.txtUserfulLife.Text;
            string ProducingArea = this.txtProducingArea.Text;
            bool Disable = this.chkDisable.Checked;
            bool IsEcalled = this.chkIsEcalled.Checked;
            string PromotionsPrice = this.txtPromotionsPrice.Text;
            DateTime PromotionsSDate = DateTime.TryParse(this.txtPromotionsSDate.Text, out result) ? result : DateTime.Now;
            DateTime PromotionsEDate = DateTime.TryParse(this.txtPromotionsEDate.Text, out result) ? result : DateTime.Now;
            string ProviderID = this.ddlProviderID.SelectedValue == "0" || this.ddlProviderID.SelectedValue == "" ? "00000000-0000-0000-0000-000000000000" : this.ddlProviderID.SelectedValue; ;
            DateTime LastInDate = DateTime.TryParse(this.txtLastInDate.Text, out result) ? result : DateTime.Now;
            string LastInPrice = this.txtLastInPrice.Text;
            string SellerRate = this.txtSellerRate.Text;
            string UserRate = this.txtUserRate.Text;
            string RecommendedRate = this.txtRecommendedRate.Text;
            string GiftsBarCode = this.txtGiftsBarCode.Text;
            string GiftsIntegral = this.txtGiftsIntegral.Text;
            bool IsGifts = this.chkIsGifts.Checked;
            string BrandID = this.ddlBrandID.SelectedValue == "0" || this.ddlBrandID.SelectedValue == "" ? "00000000-0000-0000-0000-000000000000" : this.ddlBrandID.SelectedValue; ;
            string UseDay = this.txtUseDay.Text;
            bool IsShow = this.chkIsShow.Checked;
            string Remark = this.txtRemark.Text;
            string Remark1 = this.txtRemark1.Text;
            string Remark2 = this.txtRemark2.Text;
            string Remark3 = this.txtRemark3.Text;
            string Remark4 = this.txtRemark4.Text;
            string Remark5 = this.txtRemark5.Text;
            string Remark6 = this.txtRemark6.Text;
            //int Timestamp = this.txtTimestamp.Text;
            //Stream ImageStream = UpFile.InputStream;
            //Byte[] ImageByte = new Byte[ImageLength];
            //ImageStream.Read(ImageByte, 0, ImageLength);
            //int Image = this.txtImage.Text;
            string ProductDetails = this.txtProductDetails.Text;
            string PicturePath = this.hfProductFilePath.Value;
            bool DirectMarketing = this.chkDirectMarketing.Checked;
            bool Specialofferings = this.chkSpecialofferings.Checked;
            string tOid = this.txtOid.Text;
            int Oid = 0;

            if (!int.TryParse(tOid, out Oid))
            {
                Oid = 0;
            }


            ModelGZProduct model = new ModelGZProduct();
            //model.ID = new Guid(ID);
            model.ProductTypeID = new Guid(ProductTypeID);
            model.SerialNo = SerialNo;
            model.BarCode = BarCode;
            model.PackageBarCode = PackageBarCode;
            model.ProductName = ProductName;
            model.ProductShortCutName = ProductShortCutName;
            model.ProductShortName = ProductShortName;
            model.UnitID = new Guid(UnitID);
            model.PackageUnitID = new Guid(PackageUnitID);
            model.PackageSpec = decimal.Parse(PackageSpec);
            model.ManufactureName = ManufactureName;
            model.MemberPrice = decimal.Parse(MemberPrice);
            model.SalePrice1 = decimal.Parse(SalePrice1);
            model.SalePrice2 = decimal.Parse(SalePrice2);
            model.SalePrice3 = decimal.Parse(SalePrice3);
            model.SalePrice4 = decimal.Parse(SalePrice4);
            model.SalePiecePrice1 = decimal.Parse(SalePiecePrice1);
            model.SalePiecePrice2 = decimal.Parse(SalePiecePrice2);
            model.SalePIecePrice3 = decimal.Parse(SalePIecePrice3);
            model.SalePiecePrice4 = decimal.Parse(SalePiecePrice4);
            model.MemberDiscont = decimal.Parse(MemberDiscont);
            model.MaxSalePrice = decimal.Parse(MaxSalePrice);
            model.MinSalePrice = decimal.Parse(MinSalePrice);
            model.MinSalePiecePrice = decimal.Parse(MinSalePiecePrice);
            model.MaxSalePiecePrice = decimal.Parse(MaxSalePiecePrice);
            model.IntegralSign = IntegralSign;
            model.IntegralRate = decimal.Parse(IntegralRate);
            model.NoStockCanSales = NoStockCanSales;
            model.PriceArithmeticID = new Guid(PriceArithmeticID);
            model.MaxInPrice = decimal.Parse(MaxInPrice);
            model.MaxInPiecePrice = decimal.Parse(MaxInPiecePrice);
            model.MinInPrice = decimal.Parse(MinInPrice);
            model.MinInPiecePrice = decimal.Parse(MinInPiecePrice);
            model.StockInPrice = decimal.Parse(StockInPrice);
            model.StockInPiecePrice = decimal.Parse(StockInPiecePrice);
            model.MaxStockNum = decimal.Parse(MaxStockNum);
            model.MinStockNum = decimal.Parse(MinStockNum);
            model.AverageInPrice = decimal.Parse(AverageInPrice);
            model.TotalNum = decimal.Parse(TotalNum);
            model.TotalMoney = decimal.Parse(TotalMoney);
            model.KeepQulityDate = KeepQulityDate;
            model.CreateDate = CreateDate;
            model.SpecMode = SpecMode;
            model.UserfulLife = int.Parse(UserfulLife);
            model.ProducingArea = ProducingArea;
            model.Disable = Disable;
            model.IsEcalled = IsEcalled;
            model.PromotionsPrice = decimal.Parse(PromotionsPrice);
            model.PromotionsSDate = PromotionsSDate;
            model.PromotionsEDate = PromotionsEDate;
            model.ProviderID = new Guid(ProviderID);
            model.LastInDate = LastInDate;
            model.LastInPrice = decimal.Parse(LastInPrice.Trim() == "" ? "0" : LastInPrice);
            model.SellerRate = decimal.Parse(SellerRate);
            model.UserRate = decimal.Parse(UserRate);
            model.RecommendedRate = decimal.Parse(RecommendedRate);
            model.GiftsBarCode = GiftsBarCode;
            model.GiftsIntegral = decimal.Parse(GiftsIntegral);
            model.IsGifts = IsGifts;
            model.BrandID = new Guid(BrandID);
            model.UseDay = int.Parse(UseDay);
            model.IsShow = IsShow;
            model.Remark = Remark;
            model.Remark1 = Remark1;
            model.Remark2 = Remark2;
            model.Remark3 = Remark3;
            model.Remark4 = Remark4;
            model.Remark5 = Remark5;
            model.Remark6 = Remark6;
            model.Timestamp = DateTime.Now;
            model.ProductDetails = ProductDetails;
            model.DirectMarketing = DirectMarketing;
            model.Specialofferings = Specialofferings;
            model.Oid = Oid;

            #region 上传图片
            if (this.fuProductFilePath.PostedFile.ContentLength > 0)
            {
                try
                {
                    string fullFilePath = this.fuProductFilePath.FileName;
                    string fileName = Path.GetFileName(fullFilePath);//文件名
                    string FileDate = DateTime.Now.ToString("yyyy-MM");
                    string saveFileName = "UploadFiles/" + FileDate + "/" + fileName;
                    string savePath = "UploadFiles/" + FileDate + "/";
                    string UploadFilesPath = Server.MapPath("~/");
                    if (!Directory.Exists(UploadFilesPath + savePath))
                    {
                        Directory.CreateDirectory(UploadFilesPath + savePath);
                    }
                    UploadFilesPath += saveFileName;
                    this.fuProductFilePath.SaveAs(UploadFilesPath);
                    model.PicturePath = saveFileName;
                }
                catch (Exception ex)
                {
                    model.PicturePath = "";
                }
            }
            else
            {
                model.PicturePath = PicturePath;
            }
            #endregion
            if (ID == "0")
            {
                model.ID = Guid.NewGuid();
                bllGZProduct.Add(model);
            }
            else
            {
                model.ID = new Guid(ID);
                bllGZProduct.Update(model);
            }
            Bind_DataList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Guid id = new Guid(Easp.Str.SetString(e.CommandArgument));
            string eName = Easp.Str.SetString(e.CommandName).ToLower();
            switch (eName)
            {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
            }
        }


        private void Bind_DropDownClassList()
        {
            BLLGZProductClass bllGZProductClass = new BLLGZProductClass();
            DataTable dtClassList = bllGZProductClass.GetDropDownList(0, "请选择一个分类");
            this.ddlProductTypeID.DataSource = dtClassList;
            this.ddlProductTypeID.DataTextField = "ClassName";
            this.ddlProductTypeID.DataValueField = "ClassID";
            this.ddlProductTypeID.DataBind();


            BLLGZBrand bllGZBrand = new BLLGZBrand();
            this.ddlBrandID.DataSource = bllGZBrand.GetList("").Tables[0];
            this.ddlBrandID.DataTextField = "BrandName";
            this.ddlBrandID.DataValueField = "ID";
            this.ddlBrandID.DataBind();
            this.ddlBrandID.Items.Insert(0, new ListItem("请选择", "0"));
            //
            BLLGZUnit bllGZUnit = new BLLGZUnit();
            this.ddlUnitID.DataSource = bllGZUnit.GetList("").Tables[0];
            this.ddlUnitID.DataTextField = "UnitName";
            this.ddlUnitID.DataValueField = "ID";
            this.ddlUnitID.DataBind();
            this.ddlUnitID.Items.Insert(0, new ListItem("请选择", "0"));
            //
            BLLGZPackageUnit bllGZPackageUnit = new BLLGZPackageUnit();
            this.ddlPackageUnitID.DataSource = bllGZPackageUnit.GetList("").Tables[0];
            this.ddlPackageUnitID.DataTextField = "PackageUnitName";
            this.ddlPackageUnitID.DataValueField = "ID";
            this.ddlPackageUnitID.DataBind();
            this.ddlPackageUnitID.Items.Insert(0, new ListItem("请选择", "0"));
            //
            BLLGZProviderType bllGZProviderType = new BLLGZProviderType();
            this.ddlProviderID.DataSource = bllGZProviderType.GetList("").Tables[0];
            this.ddlProviderID.DataTextField = "ProviderTypeName";
            this.ddlProviderID.DataValueField = "ID";
            this.ddlProviderID.DataBind();
            this.ddlProviderID.Items.Insert(0, new ListItem("请选择", "0"));
        }
        #endregion Bc_Product结束


        protected ModelGZProductClass GetModel_Class(object oID)
        {
            return new BLLGZProductClass().GetModel(new Guid(oID.ToString()));
        }

    }
}